package com.xly.webclick.vm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.xly.webclick.BuildConfig;
import com.xly.webclick.base.BaseViewModel;
import com.xly.webclick.model.VersionData;
import com.xly.webclick.presenter.VersionCheckPresenter;
import com.xly.webclick.utils.SpUtils;
import com.xly.webclick.vi.VersionInterFace;
import com.xly.webclick.view.HomeActivity;
import com.xly.webclick.view.LogoActivity;
import com.xly.webclick.view.SwitchActivity;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by xuleyuan on 2018/8/18
 */

public class LogoViewModel extends BaseViewModel implements VersionInterFace {

    public LogoViewModel(LogoActivity view) {
        super(view);
    }

    private long startTime;

    private VersionCheckPresenter versionCheckPresenter;

    private final int NAV_TO_SIGN = 1;

    private final int NAV_TO_HOME = 2;

    @Override
    public void onDestroy() {
        if (versionCheckPresenter != null) {
            versionCheckPresenter.detch();
            versionCheckPresenter = null;
        }

    }


    @Override
    public void onVersionCheckSuccess(VersionData versionData) {
        if (versionData.getVersion_code() > BuildConfig.VERSION_CODE) {

        } else {
            checkComplete();
        }
    }

    @Override
    public void onVersionCheckFailed() {
        checkComplete();
    }

    private synchronized void checkComplete() {

        int time = (int) (3000 - System.currentTimeMillis() + startTime);
        int nav = TextUtils.isEmpty(SpUtils.getString(SpUtils.ATV_TOKEN)) ? NAV_TO_SIGN : NAV_TO_HOME;
        if (time <= 0) {
            onCountDownOver(nav);
        } else {
            Observable.timer(time, TimeUnit.MILLISECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(i -> {
                        onCountDownOver(nav);
                    });
        }


    }

    public void onCountDownOver(int nav) {
        switch (nav) {
            case NAV_TO_SIGN:
                getActivity().startActivity(new Intent(getActivity(), SwitchActivity.class));
                getActivity().finish();
                break;
            case NAV_TO_HOME:
                getActivity().startActivity(new Intent(getActivity(), HomeActivity.class));
                getActivity().finish();
                break;
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        if (versionCheckPresenter == null) {
            versionCheckPresenter = new VersionCheckPresenter();
            versionCheckPresenter.attach(this);
        }
        versionCheckPresenter.putParams("versionCode", BuildConfig.VERSION_CODE);
        versionCheckPresenter.doRequest();
        startTime = System.currentTimeMillis();
    }

    @Override
    public void onResume() {

    }
}
